<?php
/**
 *@file comments.php
 *@author Zhe.Zhou(zhouzhe@haibao.com)
 *@date 2015-7-10 下午12:25:26
 *@brief  
 */
namespace haibao\comment\business;

use \haibao\comment\business\BusinessException;
use \haibao\comment\data\Comments as CommentsData;
use \haibao\comment\data\CommentPariseLog as CommentPariseLogData;
use \haibao\comment\model\data\CommentReportLog as CommentReportLogData;
use \haibao\cms\model\filter\ArticleProperty as ArticlePropertyFilter;
use \haibao\cms\model\filter\imagecollective\ImageCollectiveProperty as ImageCollectivePropertyFilter;
use \haibao\cms\model\filter\imagecollective\ImageCollective as ImageCollectiveFilter;
use \haibao\comment\cache\CommentCache;
use haibao\frame\data\query\Condition;
use haibao\comment\common\Auth;

class Comments extends \haibao\comment\business\Base{
	
	const HOT_COMMENTS_CACHE = 'HOT_COMMENTS_CACHE_';
	
	public static function getCommentsList($commentFilter){
		$commentData = new CommentsData();
		return  $commentData->getAll($commentFilter);
	}
	public static function getOne($commentFilter){
		$commentData = new CommentsData();
		return $commentData->getOne($commentFilter);
	}
	
	public static function verifyComment($idArr){
	    \haibao\cms\business\Base::checkOperation(\haibao\cms\business\Base::FUNC_CODE_COMMENT_VERIFY);
	    if(!$idArr){
	        throw new BusinessException('审核发生错误');
	    }
	    $commentData = new CommentsData();
	    $commentData->verifyComment($idArr);
	    \haibao\cms\business\Base::saveOperateHistory(\haibao\cms\business\Base::FUNC_CODE_COMMENT_VERIFY, $idArr);
	}
	
	public static function sensitiveComment($idArr){
	    \haibao\cms\business\Base::checkOperation(\haibao\cms\business\Base::FUNC_CODE_COMMENT_SENSITIVE);
	    if(!$idArr){
	        throw new BusinessException('审核发生错误');
	    }
	    $commentData = new CommentsData();
	    $commentData->sensitiveComment($idArr);
	    \haibao\cms\business\Base::saveOperateHistory(\haibao\cms\business\Base::FUNC_CODE_COMMENT_SENSITIVE, $idArr);
	}
	public static function updateCommentNewNum($objectId,$userId,$commentsType,$isQuestion,$isAdd,$isComment,$numInfo = array()){
	    $commentData = new CommentsData();
	    $commentData->updateCommentNewNum($objectId,$userId,$commentsType,$isQuestion,$isAdd,$isComment,$numInfo);
	}
	
	public static function addComent($model,$userInfo=array()){
		self::checkModelData($model);
		$commentData = new CommentsData();
		$commentData->add($model);
		$model->CommentContent->CommentId = $model->Id ;
		if ($model->CommentContent->ParentCommentId != 0) {
		   $model->CommentContent->Content['commentId'] = $model->Id; 
		}
		$model->CommentContent->Content = json_encode ( $model->CommentContent->Content );
		\haibao\comment\business\CommentContent::saveCommentContent($model->CommentContent);
		
		if($userInfo){
		    // 添加评论消息推送
		    if($model->ReplyCommentId){
		        // 推送回复消息
		        \haibao\www\business\message\MessageBox::addCommentMessage($model,\haibao\www\business\message\MessageBox::MESSAGE_DATA_MESSAGE_TYPE_REPLY_COMMENT,$userInfo);
		    }
		    else{
		        // 推送发布消息
		        \haibao\www\business\message\MessageBox::addCommentMessage($model,\haibao\www\business\message\MessageBox::MESSAGE_DATA_MESSAGE_TYPE_SELF_COMMENT,$userInfo);
		    }
		    
		    // 推送动态
		    if($userInfo['sso_user_public'] == \haibao\comment\common\Auth::SSO_USER_PUBLIC_USER){
		        $dataArr = array($model->CommentsTypeCode=>array($model->ObjectId));
		        $dataInfo = \haibao\www\business\message\MessageBox::getAllInfoContent($dataArr);
		        $dataInfo = \haibao\www\business\message\MessageBox::getCommentObjectData($model->ObjectId,$model->CommentsTypeCode);
		        $msgContent['MsgContent'] = array(
		            'InfoId'=>(int)$model->ObjectId,
		            'InfoType'=>\haibao\user\model\data\UserDynamics::INFO_TYPE_SELF_COMMENT,
		            'MsgType'=>\haibao\user\model\data\UserDynamics::MESSAGE_TYPE_COMMENT,
		            'Title'=>$dataInfo['Title'],
		            'InfoUrl'=>$dataInfo['Url']
		        );
		        $msgContent['UserId'] = $userInfo['_auth_user_id'];
		        \haibao\user\business\UserDynamics::addUserDynamics($msgContent);
		        	
		        // 添加积分
		        \haibao\user\common\ScoreRule::operateScore($userInfo['_auth_user_id'],\haibao\user\common\ScoreRule::TYPE_COMMENT,$model);
		    }
		}
	}
	
	public static function updateCommentById($id, $arrSet){
		$commentData = new CommentsData();
		$commentData->updateById($id, $arrSet);
	}

	public static function getCommentSource(){
		
		$sourceArr = array(
				\haibao\comment\model\data\Comments::HB_WAP=>'海报手机用户',
				\haibao\comment\model\data\Comments::HB_APP=>'海报街拍APP',
				\haibao\comment\model\data\Comments::HB_PC=>'海报PC用户',
				\haibao\comment\model\data\Comments::TX_WEIBO=>'腾讯微博',
				\haibao\comment\model\data\Comments::SINA_WEIBO=>'新浪微博',
				
		);
		return $sourceArr;
	}
	
	public static function getCommentType(){
		$typeArr = array(
				\haibao\comment\model\data\CommentType::TYPE_CODE_ARTICLE=>'资讯',
				\haibao\comment\model\data\CommentType::TYPE_CODE_PIC=>'图集',
				\haibao\comment\model\data\CommentType::TYPE_CODE_IMAGE_LIBRARY=>'图片',
				\haibao\comment\model\data\CommentType::TYPE_CODE_FEATURE=>'专题',
				\haibao\comment\model\data\CommentType::TYPE_CODE_BRAND=>'品牌',
				\haibao\comment\model\data\CommentType::TYPE_CODE_DESIGNER=>'设计师',
				\haibao\comment\model\data\CommentType::TYPE_CODE_STAR=>'明星',
				\haibao\comment\model\data\CommentType::TYPE_CODE_VIDEO=>'视频',
				\haibao\comment\model\data\CommentType::TYPE_CODE_HB_HOME_BLOG=>'博主资讯',
				\haibao\comment\model\data\CommentType::TYPE_CODE_HB_BLOG_USER=>'博主信息',
				\haibao\comment\model\data\CommentType::TYPE_CODE_STAR_PK=>'明星PK台',
				\haibao\comment\model\data\CommentType::TYPE_CODE_SNS_ANYTOPIC=>'专题/造型日历',
				\haibao\comment\model\data\CommentType::TYPE_CODE_MEIJU_ARTICLE=>'美剧文章',
				\haibao\comment\model\data\CommentType::TYPE_CODE_MEIJU_INFO=>'美剧信息',
		        \haibao\comment\model\data\CommentType::TYPE_CODE_FASHION_WEAR=>'时尚穿搭',
		        \haibao\comment\model\data\CommentType::TYPE_CODE_GOODS=>'单品',
		
		);
		return $typeArr;
	}
	
	
	public static function getCommentReportTypeSource(){
	    $ReportTypeArr = array(
	        CommentReportLogData::PORNOGRAPHY,
	        CommentReportLogData::HARASS_ABSUE,
	        CommentReportLogData::AD_FRAUD,
	        CommentReportLogData::REACTION,
	        CommentReportLogData::OTHER,
	    );
	    return $ReportTypeArr;
	}
	
	public static function getArticleCommentsList($articleId,$limit,$page){
		$commentData = new CommentsData();
		return $commentData->getArticleCommentsList($articleId,$limit,$page);
	}
	
	public static function getArticleCommentsPager($articleId,$page){
		$commentData = new CommentsData();
		return $commentData->getArticleCommentsPager($articleId,$page);
	}
	
	public static function getSensitiveWords($type){
	    $sensitiveWords = array();
		$commentData = new CommentsData();
		$sensitiveWords = $commentData->getSensitiveWords($type);
		return $sensitiveWords;
	}
	
	/**
	 * 后台删除评论
	 */
	public static function delCmsComment($commentId){
	    \haibao\cms\business\Base::checkOperation(\haibao\cms\business\Base::FUNC_CODE_COMMENT_DEL);
	    self::delComment($commentId);
	    \haibao\cms\business\Base::saveOperateHistory(\haibao\cms\business\Base::FUNC_CODE_COMMENT_DEL, false);
	}
	public static function delNewComment($commentId,$commentContentType = \haibao\comment\model\data\CommentContent::CONTENT_IS_COMMENT,$numInfo = array()){
	    $replayCommentId = 0;
	    if(!isset($commentId)){
	        throw new BusinessException('评论ID为空',self::DATA_EMPTY);
	    }
	    $commentContentFilter = new \haibao\comment\model\filter\CommentContent();
	    $commentContentData = new \haibao\comment\data\CommentContent();
	    $commentData = new CommentsData();
	    
	    $commentFilter = new \haibao\comment\model\filter\Comments();
	    $commentFilter->select(array(
	        \haibao\comment\model\filter\Comments::CONDITION_FIELD_NAME_ID,
	        \haibao\comment\model\filter\Comments::CONDITION_FIELD_NAME_IS_BACK,
	        \haibao\comment\model\filter\Comments::CONDITION_FIELD_NAME_USER_ID,
	        \haibao\comment\model\filter\Comments::CONDITION_FIELD_NAME_REPLY_COMMENT_ID,
	        \haibao\comment\model\filter\Comments::CONDITION_FIELD_NAME_REPLYNUM
	    ));
	    $commentContentFilter->select(array(
	        \haibao\comment\model\filter\CommentContent::CONDITION_FIELD_NAME_COMMENTID,
	        \haibao\comment\model\filter\CommentContent::CONDITION_FIELD_NAME_CONTENT
	    ));
	    $commentFilter->where(\haibao\comment\model\filter\CommentContent::CONDITION_FIELD_NAME_COMMENTID, Condition::CONDITION_EQUAL, $commentId,$commentContentFilter);
	    $commentFilter->leftJoin($commentContentFilter,null,\haibao\comment\model\filter\Comments::CONDITION_FIELD_NAME_ID,\haibao\comment\model\filter\CommentContent::CONDITION_FIELD_NAME_COMMENTID,\haibao\comment\model\filter\Comments::CONDITION_FIELD_NAME_COMMENTCONTENT,false);
	    $commentInfo = $commentData->getOne($commentFilter);
	    if($commentInfo){
	        if($commentContentType == \haibao\comment\model\data\CommentContent::CONTENT_IS_COMMENT){
	            if($commentInfo->ReplyCommentId == 0 && $commentInfo->ReplyNum == 0){
	                $commentData->updateById($commentId, array(\haibao\comment\model\filter\Comments::CONDITION_FIELD_NAME_STATUS=>\haibao\comment\model\data\Comments::STATUS_DELETE));
	            }else{
	                $commentContent = json_decode($commentInfo->CommentContent->Content,true);
	                if($commentContent){
                        $commentContent['content'] = '该评论已删除';
	                    $commentContent['isDel'] = 1;
	                    $commentContentData->updateCommentContentById($commentId,json_encode($commentContent));
	                }
	            }
	        }elseif($commentContentType == \haibao\comment\model\data\CommentContent::CONTENT_IS_QUESTION){
	            if($commentInfo->ReplyCommentId == 0){
	                $commentData->updateById($commentId, array(\haibao\comment\model\filter\Comments::CONDITION_FIELD_NAME_STATUS=>\haibao\comment\model\data\Comments::STATUS_DELETE));
	                \haibao\comment\business\Comments::updateCommentNewNum($commentId,0,null,$commentContentType,false,true,$numInfo);
	            }else{
	                $commentContent = json_decode($commentInfo->CommentContent->Content,true);
	                if($commentContent){
	                    $commentContent['content'] = '该回答已删除';
	                    $commentContent['isDel'] = 1;
	                    $commentContentData->updateCommentContentById($commentId,json_encode($commentContent));
	                }
	            }
	        }
	        if($commentInfo->UserId){
	            \haibao\user\common\ScoreRule::operateScore($commentInfo->UserId,\haibao\user\common\ScoreRule::TYPE_DEL_COMMENT,$commentInfo);
	        }
	    }
	    return $commentInfo->ReplyCommentId;
	}
	/**
	 * 删除评论
	 */
	public static function delComment($commentId){
		$replayCommentId = 0;
		if(!isset($commentId)){
			throw new BusinessException('评论ID为空',self::DATA_EMPTY);
		}
		
		$commentData = new CommentsData();
		$commentInfo = $commentData->getOneById($commentId);
		
		if($commentInfo){
			$replayCommentId = $commentInfo->ReplyCommentId;

			if($replayCommentId == 0){
				$commentContent = json_decode($commentInfo->Comment,true);
				if($commentContent && count($commentContent) > 1){
					if(isset($commentContent[0])){
						$commentContent[0]['content'] = '该评论已删除';
						$commentContent[0]['isDel'] = 1;
						$commentData->updateById($commentId, 
							array(
								\haibao\comment\model\filter\Comments::CONDITION_FIELD_NAME_COMMENT=>json_encode($commentContent)
								));
					}
				}else{
					$commentData->updateById($commentId, array(\haibao\comment\model\filter\Comments::CONDITION_FIELD_NAME_STATUS=>\haibao\comment\model\data\Comments::STATUS_DELETE));
				}
			}else{
				$mainCommentInfo = $commentData->getOneById($replayCommentId);
				$commentData->updateById($commentId, array(\haibao\comment\model\filter\Comments::CONDITION_FIELD_NAME_STATUS=>\haibao\comment\model\data\Comments::STATUS_DELETE));
				$mainCommentContent =  json_decode($mainCommentInfo->Comment,true);

				foreach($mainCommentContent as $key=>$comment){
					
					if($comment['commentId'] == $commentId){
						$mainCommentContent[$key]['content'] = '该评论已删除';
						$mainCommentContent[$key]['isDel'] = 1;
						$commentData->updateById($replayCommentId,
						     array(
						         \haibao\comment\model\filter\Comments::CONDITION_FIELD_NAME_COMMENT=>json_encode($mainCommentContent)
						         
						     ));
						break;
					}
				}
			}
			
			if($commentInfo->UserId){
				\haibao\user\common\ScoreRule::operateScore($commentInfo->UserId,\haibao\user\common\ScoreRule::TYPE_DEL_COMMENT,$commentInfo);
			}
		}
		return $replayCommentId;
	}
	private static function checkModelData($model){

		if(!isset($model->ObjectId)){
			throw new BusinessException('评论数据ID为空',self::DATA_EMPTY);
		}
		if(!isset($model->UserId)){
			throw new BusinessException('用户ID为空',self::DATA_EMPTY);
		}
		if(!isset($model->Comment)){
			throw new BusinessException('评论内容为空',self::DATA_EMPTY);
		}
		if(!isset($model->IP)){
			throw new BusinessException('IP地址为空',self::DATA_EMPTY);
		}
		if(!isset($model->Source)){
			throw new BusinessException('评论来源为空',self::DATA_EMPTY);
		}
		if(!isset($model->CommentsTypeCode)){
			throw new BusinessException('评论类型为空',self::DATA_EMPTY);
		}
		if(!isset($model->UserNick)){
			throw new BusinessException('用户昵称为空',self::DATA_EMPTY);
		}
	}
	
	public static function addPariseLog($model,$userInfo,$commentType){
	    if(!$model->CommentId){
	        throw new BusinessException('评论ID不能为空！');
	    }
	    $CommentPariseLogData = new CommentPariseLogData;
	    $CommentPariseLogData->add($model);
	    $commentData = new \haibao\comment\data\Comments();
	    if($commentType){
	        self::updateCommentNewNum($model,0,null,$commentType,true,false);
	    }else{
	        $commentData->addPariseNum($model);
	    }
	    
	    
	    // 添加点赞消息通知
	    $commentInfo = self::getCommentById($model->CommentId);
	    if(
	    	($userInfo['sso_user_public'] == \haibao\comment\common\Auth::SSO_USER_PUBLIC_UNREGUSER && $userInfo['_auth_user_id']!=$commentInfo->UnregUserId)||
	    	($userInfo['sso_user_public'] == \haibao\comment\common\Auth::SSO_USER_PUBLIC_USER && $userInfo['_auth_user_id']!=$commentInfo->UserId)
	    ){
		    $commentModel = new \haibao\comment\model\data\Comments();
		    $commentModel->CommentsTypeCode = $commentInfo->CommentsTypeCode;
		    $commentModel->UserId = $commentInfo->UserId;
		    $commentModel->ObjectId = $commentInfo->ObjectId;
		    $commentModel->UnregUserId = $commentInfo->UnregUserId;
		    $commentModel->MyComment = '';
		    \haibao\www\business\message\MessageBox::addCommentMessage($commentModel,\haibao\www\business\message\MessageBox::MESSAGE_DATA_MESSAGE_TYPE_PARISE_COMMENT,$userInfo);
	    }
	    
	    // DO : 推送点赞动态
	    if($userInfo['sso_user_public'] == \haibao\comment\common\Auth::SSO_USER_PUBLIC_USER){
	    	$dataArr = array($commentInfo->CommentsTypeCode=>array($commentInfo->ObjectId));
	    	$dataInfo = \haibao\www\business\message\MessageBox::getAllInfoContent($dataArr);
	    	$dataInfo = \haibao\www\business\message\MessageBox::getCommentObjectData($commentInfo->ObjectId,$commentInfo->CommentsTypeCode);
	    	$msgContent['MsgContent'] = array(
	    			'InfoId'=>(int)$commentInfo->ObjectId,
	    			'InfoType'=>\haibao\user\model\data\UserDynamics::INFO_TYPE_APPLY_SELF_COMMENT_PRAISE,
	    			'MsgType'=>\haibao\user\model\data\UserDynamics::MESSAGE_TYPE_COMMENT_PRAISE,
	    			'Title'=>$dataInfo['Title'],
	    			'InfoUrl'=>$dataInfo['Url']
	    	);
	    	$msgContent['UserId'] = $userInfo['_auth_user_id'];
			//点赞增加积分
	    	\haibao\user\common\ScoreRule::operateScore($userInfo['_auth_user_id'],\haibao\user\common\ScoreRule::TYPE_PARISE);
	 
	    	\haibao\user\business\UserDynamics::addUserDynamics($msgContent);
	    }
	}
	
	public static function getCommentById($commentId){
		if(!$commentId){
			throw new BusinessException('评论ID不能为空！');
		}

		$commentData = new CommentsData();
		return $commentData->getOneById($commentId);
	}
	public static function getTitle($titleArray){
	    
        $article = array();
        $feature = array();
        $pic = array();
        $video = array();
        $star = array();
        $american = array();
        
	    foreach ($titleArray as $key => $titleInfoList){
	     switch ($titleInfoList){
                case \haibao\comment\model\data\CommentType::TYPE_CODE_ARTICLE:
                    $article[] = $key;
                    break;
                case \haibao\comment\model\data\CommentType::TYPE_CODE_FEATURE:
                    $feature[] = $key;
                    break;
                case \haibao\comment\model\data\CommentType::TYPE_CODE_PIC:
                    $pic[] = $key;
                    break;
                case \haibao\comment\model\data\CommentType::TYPE_CODE_VIDEO:
                    $video[] = $key;
                    break;
                case \haibao\comment\model\data\CommentType::TYPE_CODE_STAR_PK:
                	$star[] = $key;
                	break;

				case \haibao\comment\model\data\CommentType::TYPE_CODE_MEIJU_INFO:
					$american[] = $key;
				break;
            }
	    }

	    
	    $titleData = new \haibao\comment\data\Comments();
	    return $titleData->getTitleInfo($article,$feature,$pic,$video,$star,$american);
	    
	}
	public static function updateMyComment($id,$arrSet){
        $commentData = new CommentsData();
		$commentData->updateById($id, $arrSet);
	}
    public static function getCommentReplyNameList($myCommentReplyIdList){
        $getReplyNameData = new \haibao\comment\data\Comments();
        return $getReplyNameData->getReplyNameList($myCommentReplyIdList);
    }
	public static function updateCommentReplyNum($commentId){
		if(!$commentId){
			throw new BusinessException('评论ID不能为空！');
		}
		$commentData = new \haibao\comment\data\Comments();
		$commentData->addReplyNum($commentId);
	}
	
	public static function getHotCommentList($commentType,$channel = 0){

		$titleArray = $hotCommentList = array();
		if($channel){
			$cacheKey = self::HOT_COMMENTS_CACHE.$commentType.$channel;
		}else{
			$cacheKey = self::HOT_COMMENTS_CACHE.$commentType;
		}
		if(CommentCache::getCrontabCache($cacheKey)){
			$hotCommentList = json_decode(CommentCache::getCrontabCache($cacheKey));
		}else{
			switch ($commentType){
	            case \haibao\comment\model\data\CommentType::TYPE_CODE_ARTICLE:
	                $articlePropertyFilter = new ArticlePropertyFilter();
	                $articlePropertyFilter->select(array(
	                    ArticlePropertyFilter::CONDITION_FIELD_NAME_ID,
	                    ArticlePropertyFilter::CONDITION_FIELD_NAME_ARTICLE_ID,
	                    ArticlePropertyFilter::CONDITION_FIELD_NAME_ARTICLE_COMMENT_NUM
	                ));
	                
	                $articlePropertyFilter->order(ArticlePropertyFilter::CONDITION_FIELD_NAME_ARTICLE_ID,false);
	                $articlePropertyFilter->order(ArticlePropertyFilter::CONDITION_FIELD_NAME_ARTICLE_COMMENT_NUM,false);
	                $articlePropertyFilter->limit(10);
	                $articlePropertyFilter->where(ArticlePropertyFilter::CONDITION_FIELD_NAME_PC_CLICK_NUM, Condition::CONDITION_GREATER, 0);
	                $hotCommentList =\haibao\cms\business\ArticleProperty::getArticleCommentNum($articlePropertyFilter);
	                foreach ($hotCommentList as $titleInfo){
	                    $titleArray[$titleInfo->ArticleId] = $commentType;
	                }
	                break;
	            case \haibao\comment\model\data\CommentType::TYPE_CODE_FEATURE:
	                $featurePublishFilter = new \haibao\cms\model\filter\FeaturePublish();
	                $featurePublishFilter->select(array(
	                    \haibao\cms\model\filter\FeaturePublish::CONDITION_FIELD_NAME_ID,
	                    \haibao\cms\model\filter\FeaturePublish::CONDITION_FIELD_NAME_SERIAL_ID,
	                    \haibao\cms\model\filter\FeaturePublish::CONDITION_FIELD_NAME_COMMENT_COUNT,
	                    \haibao\cms\model\filter\FeaturePublish::CONDITION_FIELD_NAME_PUBLISH_TIME,
	                    \haibao\cms\model\filter\FeaturePublish::CONDITION_FIELD_NAME_IS_H5
	                ));
	                $featurePublishFilter->limit(10);
	                $featurePublishFilter->where(\haibao\cms\model\filter\FeaturePublish::CONDITION_FIELD_NAME_IS_H5,\haibao\frame\data\query\Condition::CONDITION_EQUAL,0);
	                $featurePublishFilter->order(\haibao\cms\model\filter\FeaturePublish::CONDITION_FIELD_NAME_PUBLISH_TIME,false);
	                $hotCommentList =\haibao\cms\business\featurePublish::getFeatureNum($featurePublishFilter);
	                foreach ($hotCommentList as $titleInfo){
	                    $titleArray[$titleInfo->Id] = $commentType;
	                }
	                break;
	            case \haibao\comment\model\data\CommentType::TYPE_CODE_PIC:
	                $imageCollectiveFilter = new ImageCollectiveFilter();
	                $imageCollectiveFilter->select(array(
	                    ImageCollectiveFilter::CONDITION_FIELD_NAME_ID
	                ));
	                $imageCollectivePropertyFilter = new ImageCollectivePropertyFilter();
	                $imageCollectivePropertyFilter->select(array(
	                    ImageCollectivePropertyFilter::CONDITION_FIELD_NAME_ID,
	                    ImageCollectivePropertyFilter::CONDITION_FIELD_NAME_IMAGE_COMMENT_NUM,
	                ));
	                $imageCollectiveFilter->limit(0,10);
	                $imageCollectiveFilter->order(ImageCollectivePropertyFilter::CONDITION_FIELD_NAME_IMAGE_COMMENT_NUM,false,$imageCollectivePropertyFilter);
	                $imageCollectiveFilter->order(ImageCollectiveFilter::CONDITION_FIELD_NAME_IMAGE_COLLECTIVE_PUBLISHTIME,false);
	                $imageCollectiveFilter->leftJoin($imageCollectivePropertyFilter,null,ImageCollectiveFilter::CONDITION_FIELD_NAME_ID,ImageCollectivePropertyFilter::CONDITION_FIELD_NAME_IMAGE_COLLECTIVE_ID,ImageCollectiveFilter::CONDITION_FIELD_NAME_IMAGE_COLLECTIVE_PRO,false);
	                $hotCommentList = \haibao\cms\business\imagecollective\ImageCollective::getImageCollectiveByFilter($imageCollectiveFilter);
	                if($hotCommentList){
	                    foreach($hotCommentList as $imageCollective){
	                        $titleArray[$imageCollective->Id] = $commentType;
	                    }
	                }
	               
	                break;
	            case \haibao\comment\model\data\CommentType::TYPE_CODE_VIDEO:
	                $videoPropertyFilter = new \haibao\cms\model\filter\VideoProperty();
	                $videoPropertyFilter->select(array(
	                    \haibao\cms\model\filter\VideoProperty::CONDITION_FIELD_NAME_ID,
	                    \haibao\cms\model\filter\VideoProperty::CONDITION_FIELD_NAME_VIDEOID,
	                    \haibao\cms\model\filter\VideoProperty::CONDITION_FIELD_NAME_COMMENTNUM
	                ));
	                
	                $videoFilter = new \haibao\cms\model\filter\Video();
	                $videoFilter->select(array(
	                    \haibao\cms\model\filter\Video::CONDITION_FIELD_NAME_ID,
	                    \haibao\cms\model\filter\Video::CONDITION_FIELD_NAME_PUBLISHDATE
	                ));
	                
	                $videoFilter->limit(10);
	                $videoFilter->order(\haibao\cms\model\filter\VideoProperty::CONDITION_FIELD_NAME_COMMENTNUM,false,$videoPropertyFilter);
	                $videoFilter->leftJoin($videoPropertyFilter,null,\haibao\cms\model\filter\Video::CONDITION_FIELD_NAME_ID, \haibao\cms\model\filter\VideoProperty::CONDITION_FIELD_NAME_VIDEOID,\haibao\cms\model\filter\Video::CONDITION_FIELD_NAME_PROPERTY,false);
	                $hotCommentList =\haibao\cms\business\Video::getVideoNum($videoFilter);
	                foreach ($hotCommentList as $titleInfo){
	                    $titleArray[$titleInfo->Id] = $commentType;
	                }
	                break;
	        }
	        if($titleArray){
		        $getTitle = self::getTitle($titleArray);
		        foreach($hotCommentList as $key=>$hotComment){
		            switch ($commentType){
		                case \haibao\comment\model\data\CommentType::TYPE_CODE_ARTICLE:
		                      if(!empty($getTitle[$hotComment->ArticleId])){
		                       $hotComment->PublicTitle = $getTitle[$hotComment->ArticleId]['Title'];
		                       $hotComment->AccessUrl = \haibao\classlibrary\comment\Config::getConfig(\haibao\classlibrary\comment\Config::CLASSLIBRARY_CONFIG_HAIBAO_URL)."/article/".$getTitle[$hotComment->ArticleId]['PublishId'].".htm";
		                      }
		                    break;
		                case \haibao\comment\model\data\CommentType::TYPE_CODE_FEATURE:
		                    
		                    if(!empty($getTitle[$hotComment->Id])){
		                        $hotComment->PublicTitle = $getTitle[$hotComment->Id]['FeatureName'];
		                        $hotComment->AccessUrl  = $getTitle[$hotComment->Id]['Url'];
		                    }
		                    break;
		                case \haibao\comment\model\data\CommentType::TYPE_CODE_PIC:
		                    
		                    if(!empty($getTitle[$hotComment->Id])){
		                        $hotComment->PublicTitle  = $getTitle[$hotComment->Id]['Title'];
		                        $hotComment->AccessUrl =\haibao\classlibrary\comment\Config::getConfig(\haibao\classlibrary\comment\Config::CLASSLIBRARY_CONFIG_HOST_PIC)."/pics/".$hotComment->Id.".htm";
		                    }
		                    break;
		                 case \haibao\comment\model\data\CommentType::TYPE_CODE_VIDEO:
		                     
		                     if(!empty($getTitle[$hotComment->Id])){
		                        $hotComment->PublicTitle  = $getTitle[$hotComment->Id]['Title'];
		                        $hotComment->AccessUrl  = \haibao\classlibrary\comment\Config::getConfig(\haibao\classlibrary\comment\Config::CLASSLIBRARY_CONFIG_HOST_EXCLUSIVE).'/play/'.$hotComment->Id.'.htm';
		                        
		                     }
		                    break;
	                    case \haibao\comment\model\data\CommentType::TYPE_CODE_MEIJU_INFO:
	                    	 
	                    	if(!empty($getTitle[$hotComment->Id])){
	                    		$hotComment->PublicTitle  = $getTitle[$hotComment->Id]['NameCH'];
	                    		$hotComment->AccessUrl  = \haibao\classlibrary\comment\Config::getConfig(\haibao\classlibrary\comment\Config::CLASSLIBRARY_CONFIG_TV_HOST).'meiju/'.$hotComment->Id;
	                    
	                    	}
	                    	break;
		            }
		            $hotCommentList[$key] = $hotComment;
	        	}
				CommentCache::setCrontabCache($cacheKey, 7200,json_encode($hotCommentList));
			}
		}
		return $hotCommentList;
	}
	public static function getPraiseList($praiseFilter){
	    $commentData = new CommentsData();
	    return $commentData->getAll($praiseFilter);
	}
	public static function getCommentReplyListById($commentId){
	    if(!$commentId){
	        throw new BusinessException('评论ID不能为空！');
	    }
	    $commentId = (array)$commentId;
	    $commentFilter = new \haibao\comment\model\filter\Comments();
	    $commentContentFilter = new \haibao\comment\model\filter\CommentContent();
	    $commentFilter->select(array(
	        \haibao\comment\model\filter\Comments::CONDITION_FIELD_NAME_ID,
	        \haibao\comment\model\filter\Comments::CONDITION_FIELD_NAME_USER_ID,
	        \haibao\comment\model\filter\Comments::CONDITION_FIELD_NAME_OBJECT_ID,
	        \haibao\comment\model\filter\Comments::CONDITION_FIELD_NAME_IS_VERIFY,
	        \haibao\comment\model\filter\Comments::CONDITION_FIELD_NAME_STATUS,
	        \haibao\comment\model\filter\Comments::CONDITION_FIELD_NAME_REPLY_COMMENT_ID,
	        \haibao\comment\model\filter\Comments::CONDITION_FIELD_NAME_PRAISE,
	        \haibao\comment\model\filter\Comments::CONDITION_FIELD_NAME_SOURCE,
	        \haibao\comment\model\filter\Comments::CONDITION_FIELD_NAME_FLOOR_ID,
	        \haibao\comment\model\filter\Comments::CONDITION_FIELD_NAME_USER_NICK,
	        \haibao\comment\model\filter\Comments::CONDITION_FIELD_NAME_REPLY_USER_ID,
	        \haibao\comment\model\filter\Comments::CONDITION_FIELD_NAME_REPLYNUM,
	        \haibao\comment\model\filter\Comments::CONDITION_FIELD_NAME_COMMENTS_TYPE_CODE,
	        \haibao\comment\model\filter\Comments::CONDITION_FIELD_NAME_COMMENT_IDS,
	        \haibao\comment\model\filter\Comments::CONDITION_FIELD_NAME_MY_COMMENT
	    ));
	    $commentContentFilter->select(array(
	        \haibao\comment\model\filter\CommentContent::CONDITION_FIELD_NAME_ID,
	        \haibao\comment\model\filter\CommentContent::CONDITION_FIELD_NAME_COMMENTID,
	        \haibao\comment\model\filter\CommentContent::CONDITION_FIELD_NAME_CONTENT,
	        \haibao\comment\model\filter\CommentContent::CONDITION_FIELD_NAME_ISADOPT,
	        \haibao\comment\model\filter\CommentContent::CONDITION_FIELD_NAME_ISQUESTION,
	        \haibao\comment\model\filter\CommentContent::CONDITION_FIELD_NAME_PARENTCOMMENTID,
	        \haibao\comment\model\filter\CommentContent::CONDITION_FIELD_NAME_CREATETIME
	    ));
	    $commentFilter->order(\haibao\comment\model\filter\Comments::CONDITION_FIELD_NAME_ID,true);
// 	    $commentFilter->order(\haibao\comment\model\filter\CommentContent::CONDITION_FIELD_NAME_ISADOPT,false,$commentContentFilter);
	    $commentFilter->where(\haibao\comment\model\filter\Comments::CONDITION_FIELD_NAME_REPLY_COMMENT_ID,Condition::CONDITION_IN,$commentId);
	    $commentFilter->leftJoin($commentContentFilter,null,\haibao\comment\model\filter\Comments::CONDITION_FIELD_NAME_ID,\haibao\comment\model\filter\CommentContent::CONDITION_FIELD_NAME_COMMENTID,\haibao\comment\model\filter\Comments::CONDITION_FIELD_NAME_COMMENTCONTENT,false);
	    $commentList = array();
	    $commentData = new CommentsData();
	    $comments = $commentData->getAll($commentFilter);
	    if($comments){
	        foreach($comments as $comment){
	            $content = stripslashes($comment->CommentContent->Content);
	            $content = preg_replace_callback("/http:\/\/(.*?).gif/","self::pregContentEmot",$content);
	            $commentList[$comment->ReplyCommentId][$comment->Id] = $comment;
	        }
	    }
	    return $commentList;
	}
	public static function getCommentListById($commentId){
		if(!$commentId){
			throw new BusinessException('评论ID不能为空！');
		}
		$commentId = (array)$commentId;
		$commentFilter = new \haibao\comment\model\filter\Comments();
		$commentFilter->select(array(
				\haibao\comment\model\filter\Comments::CONDITION_FIELD_NAME_ID,
		        \haibao\comment\model\filter\Comments::CONDITION_FIELD_NAME_USER_ID,
				\haibao\comment\model\filter\Comments::CONDITION_FIELD_NAME_OBJECT_ID,
				\haibao\comment\model\filter\Comments::CONDITION_FIELD_NAME_IS_VERIFY,
		        \haibao\comment\model\filter\Comments::CONDITION_FIELD_NAME_STATUS,
				\haibao\comment\model\filter\Comments::CONDITION_FIELD_NAME_REPLY_COMMENT_ID,
				\haibao\comment\model\filter\Comments::CONDITION_FIELD_NAME_COMMENT,
    		    \haibao\comment\model\filter\Comments::CONDITION_FIELD_NAME_PRAISE,
    		    \haibao\comment\model\filter\Comments::CONDITION_FIELD_NAME_SOURCE,
		        \haibao\comment\model\filter\Comments::CONDITION_FIELD_NAME_REPLYNUM,
    		    \haibao\comment\model\filter\Comments::CONDITION_FIELD_NAME_COMMENTS_TYPE_CODE
		));
		
		$commentFilter->where(\haibao\comment\model\filter\Comments::CONDITION_FIELD_NAME_ID,Condition::CONDITION_IN,$commentId);
// 		$commentFilter->where(\haibao\comment\model\filter\Comments::CONDITION_FIELD_NAME_STATUS,Condition::CONDITION_EQUAL,\haibao\comment\model\data\Comments::STATUS_OK);
// 		$commentFilter->where(\haibao\comment\model\filter\Comments::CONDITION_FIELD_NAME_REPLY_COMMENT_ID,Condition::CONDITION_GREATER,0);

		$commentList = array();
		$commentData = new CommentsData();
		$comments = $commentData->getAll($commentFilter);
		if($comments){ 
			foreach($comments as $comment){
			    $content = stripslashes($comment->Comment);
			    $content = preg_replace_callback("/http:\/\/(.*?).gif/","self::pregContentEmot",$content);
				$commentList[$comment->Id] = $comment->toArray();
			}
		}
		
		return $commentList;
	}
	public static function checkCaptcha($captcha,$commentId = 0,$isSubjuct = false){
	    $captcha = strtolower( $captcha );
	    \haibao\frame\http\Cookie::$cookie = null;
	    
	    $ret = true;
	    if(mb_strlen($captcha) == 0){
	        throw new BusinessException('请输入正确的验证码！');
	    }
	    \haibao\frame\http\Cookie::$cookie = null;
	    $sessionId = \haibao\frame\http\Cookie::get('__captcha_comment');
	    $rightCaptcha = \haibao\comment\cache\CommentCache::getCrontabCache($sessionId);
 	    if($captcha != $rightCaptcha){
 	        throw new BusinessException('请输入正确的验证码！');
 	    }
 	    if($isSubjuct){
 	    	\haibao\comment\cache\CommentCache::delCrontabCache($sessionId);
 	    }
	    return $ret;
	}

	/*
	 * 用户数据合并
	 */
	public static function mergeUserData($userId,$nickname){
		
		if(!$userId){
			throw new BusinessException('用户ID不能为空！');
		}
		// DO : 获取游客信息
		$unRegUserId = Auth::getUnRegUserId();
		$data = new \haibao\comment\data\Comments();
		$data->mergeUserData((int)$userId,$unRegUserId,$nickname);
		return $unRegUserId;
	}
	
	private static function pregContentEmot($match){
	    //return str_replace("mt0","cdn0",$match[0]);
	    return str_replace("mt0","c1",$match[0]);
	}
	
	public static function getCommentContentType($commentId){
	    $commentFilter = new \haibao\comment\model\filter\Comments();
	    $commentContentFilter = new \haibao\comment\model\filter\CommentContent();
	    $commentFilter->select(array(
	        \haibao\comment\model\filter\Comments::CONDITION_FIELD_NAME_ID,
	        \haibao\comment\model\filter\Comments::CONDITION_FIELD_NAME_CHANNEL
	    ));
	    $commentContentFilter->select(array(
	        \haibao\comment\model\filter\CommentContent::CONDITION_FIELD_NAME_ID,
	        \haibao\comment\model\filter\CommentContent::CONDITION_FIELD_NAME_COMMENTID,
	        \haibao\comment\model\filter\CommentContent::CONDITION_FIELD_NAME_ISQUESTION
	    ));
	    $commentFilter->where(\haibao\comment\model\filter\Comments::CONDITION_FIELD_NAME_ID,Condition::CONDITION_EQUAL,$commentId );
	    $commentFilter->leftJoin($commentContentFilter,null,\haibao\comment\model\filter\Comments::CONDITION_FIELD_NAME_ID,\haibao\comment\model\filter\CommentContent::CONDITION_FIELD_NAME_COMMENTID,\haibao\comment\model\filter\Comments::CONDITION_FIELD_NAME_COMMENTCONTENT,false);
	    $commentData = new CommentsData();
	    $commentInfo = $commentData->getAll($commentFilter);
	    $channel = 0;
	    $commentContentType = \haibao\comment\model\data\CommentContent::CONTENT_IS_COMMENT;
	    if($commentInfo){
	        foreach ($commentInfo as $key=>$val){
	            $channel = $val->Channel;
	            $commentContentType = $val->CommentContent->IsQuestion;
	        }
	    }
	    return array(
	        'channel'=>$channel,
	        'type'=>$commentContentType
	    );
	}
}